import type { Ref } from 'vue';
import type { CRUDAction, CRUDFN, FN, FNB, PopupOpenFn } from '@/types/global';
import type { UniPopupType } from '@uni-helper/uni-ui-types';
import { ref, computed } from 'vue';

export const useCRUD = (afterText : string) : [Ref<string>, { action : Ref<CRUDAction>, setAction : (action : CRUDAction) => void; }] => {
	const action = ref<CRUDAction>('create');
	const title = computed(() => {
		let preText = '';
		switch (action.value) {
			case 'create':
				preText = '新增';
				break;
			case 'update':
				preText = '编辑';
				break;
			case 'delete':
				preText = '删除';
				break;
		}
		return `${preText}${afterText}`
	});
	const setAction = (actionValue : CRUDAction) => {
		action.value = actionValue;
	}
	return [title, { action, setAction }];
}

export const usePopup = () : [Ref, { open : PopupOpenFn, close : FN, openPopup : PopupOpenFn, closePopup : FN }] => {
	const popupRef = ref();
	const open = (type ?: UniPopupType) => {
		popupRef?.value?.open(type);
	}
	const close = () => {
		popupRef?.value?.close();
	}
	return [popupRef, {
		open,
		close,
		openPopup: open,
		closePopup: close,
	}];
}

export const useDialog = () : [Ref, { loading : Ref<boolean>, setLoading : FNB, setDialogLoading : FNB, dialogLoading : Ref<boolean>, open : FN, close : FN, openDialog : FN, closeDialog : FN }] => {
	const loading = ref(false);
	const dialogRef = ref();
	const open = () => {
		dialogRef?.value?.open();
	}
	const close = () => {
		dialogRef?.value?.close();
	}
	function setLoading(boolean : boolean) {
		loading.value = boolean;
	}
	return [dialogRef, {
		loading,
		dialogLoading: loading,
		setLoading,
		setDialogLoading: setLoading,
		open,
		close,
		openDialog: open,
		closeDialog: close,
	}];
}

export const useLocalityFiltratePopup = () : [Ref, { open : FN, close : FN, toggle : FN, openLocalityFiltratePopup : FN, closeLocalityFiltratePopup : FN, toggleLocalityFiltratePopup : FN }] => {
	const localityFiltratePopupRef = ref();
	const open = () => {
		localityFiltratePopupRef.value.open();
	}
	const close = () => {
		localityFiltratePopupRef.value.close();
	}
	const toggle = () => {
		localityFiltratePopupRef.value.toggle();
	}

	const reset = () => {
		localityFiltratePopupRef.value.reset();
	}
	return [localityFiltratePopupRef, {
		open,
		close,
		toggle,
		openLocalityFiltratePopup: open,
		closeLocalityFiltratePopup: close,
		toggleLocalityFiltratePopup: toggle
	}];
}


export const useStartLotteryPopup = () : [Ref, { open : FN, openStartLotteryPopup : FN }] => {
	const startLotteryRef = ref();
	const open = () => {
		startLotteryRef.value.open();
	}
	return [startLotteryRef, {
		open,
		openStartLotteryPopup: open
	}];
}


export const useSignUpStatisticsPopup = () : [Ref, { open : FN, openSignUpStatisticsPopup : FN }] => {
	const signUpStatisticsRef = ref();
	const open = () => {
		signUpStatisticsRef.value.open();
	}
	return [signUpStatisticsRef, {
		open,
		openSignUpStatisticsPopup: open
	}];
}

export const useSeatStatisticsPopup = () : [Ref, { open : FN, openSeatStatisticsPopup : FN }] => {
	const seatStatisticsRef = ref();
	const open = () => {
		seatStatisticsRef.value.open();
	}
	return [seatStatisticsRef, {
		open,
		openSeatStatisticsPopup: open
	}];
}

export const useRecycleFishPopup = () : [Ref, { open : FN, openRecycleFishPopup : FN }] => {
	const recycleFishRef = ref();
	const open = () => {
		recycleFishRef.value.open();
	}
	return [recycleFishRef, {
		open,
		openRecycleFishPopup: open
	}];
}

export const useRefundPopup = () : [Ref, { open : FN, openRefundPopup : FN }] => {
	const refundRef = ref();
	const open = () => {
		refundRef.value.open();
	}
	return [refundRef, {
		open,
		openRefundPopup: open
	}];
}

export const useQuickPositionSeatPopup = () : [Ref, { open : FN, openQuickPositionSeatPopup : FN, close : FN, closeQuickPositionSeatPopup : FN }] => {
	const quickPositionSeatRef = ref();
	const open = () => {
		quickPositionSeatRef.value.open();
	}

	const close = () => {
		quickPositionSeatRef.value.close();
	}
	return [quickPositionSeatRef, {
		open,
		openQuickPositionSeatPopup: open,
		close,
		closeQuickPositionSeatPopup: close
	}];
}

export const useQuickRecycleFishPopup = () : [Ref, { open : FN, openQuickRecycleFishPopup : FN, close : FN, closeQuickRecycleFishPopup : FN }] => {
	const quickRecycleFishRef = ref();
	const open = () => {
		quickRecycleFishRef.value.open();
	}

	const close = () => {
		quickRecycleFishRef.value.close();
	}
	return [quickRecycleFishRef, {
		open,
		openQuickRecycleFishPopup: open,
		close,
		closeQuickRecycleFishPopup: close
	}];
}

export const useQuickConsumePopup = () : [Ref, { open : FN, openQuickConsumePopup : FN, close : FN, closeQuickConsumePopup : FN }] => {
	const quickConsumeRef = ref();
	const open = () => {
		quickConsumeRef.value.open();
	}

	const close = () => {
		quickConsumeRef.value.close();
	}
	return [quickConsumeRef, {
		open,
		openQuickConsumePopup: open,
		close,
		closeQuickConsumePopup: close
	}];
}

export const useSettlementDetailsPopup = () : [Ref, { open : FN, openSettlementDetailsPopup : FN, close : FN, closeSettlementDetailsPopup : FN }] => {
	const settlementDetailsRef = ref();
	const open = () => {
		settlementDetailsRef.value.open();
	}

	const close = () => {
		settlementDetailsRef.value.close();
	}
	return [settlementDetailsRef, {
		open,
		openSettlementDetailsPopup: open,
		close,
		closeSettlementDetailsPopup: close
	}];
}

export const useSettlementListPopup = () : [Ref, { open : FN, openSettlementListPopup : FN, close : FN, closeSettlementListPopup : FN }] => {
	const settlementListPopupRef = ref();
	const open = () => {
		settlementListPopupRef.value.open();
	}

	const close = () => {
		settlementListPopupRef.value.close();
	}
	return [settlementListPopupRef, {
		open,
		openSettlementListPopup: open,
		close,
		closeSettlementListPopup: close
	}];
}

export const useSeatSettingPopup = () : [Ref, { open : FN, openSeatSettingPopup : FN, close : FN, closeSeatSettingPopup : FN }] => {
	const seatSettingPopupRef = ref();
	const open = () => {
		seatSettingPopupRef.value.open();
	}

	const close = () => {
		seatSettingPopupRef.value.close();
	}
	return [seatSettingPopupRef, {
		open,
		openSeatSettingPopup: open,
		close,
		closeSeatSettingPopup: close
	}];
}


export const useOrderDetailPopup = () : [Ref, { open : FN, openOrderDetailPopup : FN, close : FN, closeOrderDetailPopup : FN }] => {
	const orderDetailPopupRef = ref();
	const open = () => {
		orderDetailPopupRef.value.open();
	}

	const close = () => {
		orderDetailPopupRef.value.close();
	}
	return [orderDetailPopupRef, {
		open,
		openOrderDetailPopup: open,
		close,
		closeOrderDetailPopup: close
	}];
}


export const useRefundTicketDialog = () : [Ref, { open : FN, openRefundTicketDialog : FN, close : FN, closeRefundTicketDialog : FN }] => {
	const refundTicketDialogRef = ref();
	const open = () => {
		refundTicketDialogRef.value.open();
	}

	const close = () => {
		refundTicketDialogRef.value.close();
	}
	return [refundTicketDialogRef, {
		open,
		openRefundTicketDialog: open,
		close,
		closeRefundTicketDialog: close
	}];
}

export const useFishingGroundFailAuditDialog = () : [Ref, { open : FN, openFishingGroundFailAuditDialog : FN, close : FN, closeFishingGroundFailAuditDialog : FN }] => {
	const fishingGroundFailAuditDialogRef = ref();
	const open = () => {
		fishingGroundFailAuditDialogRef.value.open();
	}

	const close = () => {
		fishingGroundFailAuditDialogRef.value.close();
	}
	return [fishingGroundFailAuditDialogRef, {
		open,
		openFishingGroundFailAuditDialog: open,
		close,
		closeFishingGroundFailAuditDialog: close
	}];
}

export const useCrudFishpondPopup = () : [Ref, { open : CRUDFN, openCrudFishpondPopup : CRUDFN, close : FN, closeCrudFishpondPopup : FN }] => {
	const crudFishpondPopupRef = ref();
	const open : CRUDFN = (args) => {
		crudFishpondPopupRef.value.open(args);
	}

	const close = () => {
		crudFishpondPopupRef.value.close();
	}
	return [crudFishpondPopupRef, {
		open,
		openCrudFishpondPopup: open,
		close,
		closeCrudFishpondPopup: close
	}];
}

export const useFishpondDetail = () : [Ref, { open : FN, openFishpondDetail : FN, close : FN, closeFishpondDetail : FN }] => {
	const fishpondDetailRef = ref();
	const open = () => {
		fishpondDetailRef.value.open();
	}

	const close = () => {
		fishpondDetailRef.value.close();
	}
	return [fishpondDetailRef, {
		open,
		openFishpondDetail: open,
		close,
		closeFishpondDetail: close
	}];
}

export const useCrudSeatPopup = () : [Ref, { open : FN, openCrudSeatPopup : FN, close : FN, closeCrudSeatPopup : FN }] => {
	const crudSeatPopupRef = ref();
	const open = () => {
		crudSeatPopupRef.value.open();
	}

	const close = () => {
		crudSeatPopupRef.value.close();
	}
	return [crudSeatPopupRef, {
		open,
		openCrudSeatPopup: open,
		close,
		closeCrudSeatPopup: close
	}];
}

export const useCrudMarkfishPopup = () : [Ref, { open : CRUDFN, openCrudMarkfishPopup : CRUDFN, close : FN, closeCrudMarkfishPopup : FN }] => {
	const crudMarkfishPopupRef = ref();
	const open : CRUDFN = (args) => {
		crudMarkfishPopupRef.value.open(args);
	}

	const close = () => {
		crudMarkfishPopupRef.value.close();
	}
	return [crudMarkfishPopupRef, {
		open,
		openCrudMarkfishPopup: open,
		close,
		closeCrudMarkfishPopup: close
	}];
}

export const useHandlerJoinTeamPopup = () : [Ref, { open : FN, openHandlerJoinTeamPopup : FN, close : FN, closeHandlerJoinTeamPopup : FN }] => {
	const handlerJoinTeamPopupRef = ref();
	const open : CRUDFN = (args) => {
		handlerJoinTeamPopupRef.value.open(args);
	}

	const close = () => {
		handlerJoinTeamPopupRef.value.close();
	}
	return [handlerJoinTeamPopupRef, {
		open,
		openHandlerJoinTeamPopup: open,
		close,
		closeHandlerJoinTeamPopup: close
	}];
}

export const useCorrelationShopPopup = () : [Ref, { open : FN, openCorrelationShopPopup : FN, close : FN, closeCorrelationShopPopup : FN }] => {
	const correlationShopPopupRef = ref();
	const open : CRUDFN = (args) => {
		correlationShopPopupRef.value.open(args);
	}

	const close = () => {
		correlationShopPopupRef.value.close();
	}
	return [correlationShopPopupRef, {
		open,
		openCorrelationShopPopup: open,
		close,
		closeCorrelationShopPopup: close
	}];
}

export const useCorrelationTeamPopup = () : [Ref, { open : FN, openCorrelationTeamPopup : FN, close : FN, closeCorrelationTeamPopup : FN }] => {
	const correlationTeamPopupRef = ref();
	const open : CRUDFN = (args) => {
		correlationTeamPopupRef.value.open(args);
	}

	const close = () => {
		correlationTeamPopupRef.value.close();
	}
	return [correlationTeamPopupRef, {
		open,
		openCorrelationTeamPopup: open,
		close,
		closeCorrelationTeamPopup: close
	}];
}

export const useCrudGoodsPopup = () : [Ref, { open : CRUDFN, openCrudGoodsPopup : CRUDFN, close : FN, closeCrudGoodsPopup : FN }] => {
	const crudGoodsPopupRef = ref();
	const open : CRUDFN = (args) => {
		crudGoodsPopupRef.value.open(args);
	}

	const close = () => {
		crudGoodsPopupRef.value.close();
	}
	return [crudGoodsPopupRef, {
		open,
		openCrudGoodsPopup: open,
		close,
		closeCrudGoodsPopup: close
	}];
}